package com.htc.dlc;

import java.util.ArrayList;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.htc.adapter.SpaAdapter;
import com.htc.adapter.SpinnerAdapter;
import com.htc.menu.MenuController;
import com.htc.menu.MenuController.MenuSelectedListener;
import com.htc.model.dto.SpaChildDTO;
import com.htc.model.dto.SpaParentDTO;
import com.htc.model.utils.SpasModel;
import com.htc.slidingmenu.library.CirclePageIndicator;
import com.htc.slidingmenu.library.PageIndicator;
import com.htc.utils.CommonActions;

@SuppressLint("ResourceAsColor")
public class SpaActivity extends Activity implements MenuSelectedListener {
	MenuController menuController;
	View menuView;
	View menuViewSecondary;
	LinearLayout blurimageView;
	CommonActions ca;
	public ExpandableListView spaElv;
	TextView tv_title_spa;
	ArrayList<SpaChildDTO> arrChild;
	ArrayList<SpaChildDTO> spaChildList;
	// ArrayList<SpaParentDTO> spaParentList;
	SpaAdapter adapter;
	View footer;
	View header;
	Handler handler;
	public static final int DELAY = 5000;
	ViewPager mPager;
	PageIndicator mIndicator;
	// Spinner spinner;
	SpinnerAdapter spAdapter;
	TextView tv_actionbar_title;
	EditText edSpa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spa);
		this.ca = new CommonActions(this);
		initViews();
		this.menuController = new MenuController(this.menuView,
				this.menuViewSecondary, this.blurimageView, this,
				getBaseContext(), SpaActivity.this);

		ImagePagerAdapter adapterimg = new ImagePagerAdapter();
		mPager.setAdapter(adapterimg);

		mIndicator.setViewPager(mPager);

		final SpasModel md = SpasModel.getInstance();
		spaChildList = md.getAllSpaSections(SpaActivity.this);
		// spaParentList = md.getAllSpaCategory(SpaActivity.this);
		arrChild = new ArrayList<SpaChildDTO>();
		adapter = new SpaAdapter(SpaActivity.this, 0, spaChildList);
		spaElv.setAdapter(adapter);
		tv_title_spa.setText("All Treatments");
		arrChild = new ArrayList<SpaChildDTO>();
		edSpa.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// SpasModel md = SpasModel.getInstance();

				if (!arrChild.contains(null)) {
					String text = edSpa.getText().toString()
							.toLowerCase(Locale.getDefault());
					filter(text);
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		// spAdapter.selectedString = "All Treatments";

		// spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
		//
		// @Override
		// public void onItemSelected(AdapterView<?> parent, View view,
		// int position, long id) {
		//
		// if (((SpaParentDTO) SpaActivity.this.spaParentList
		// .get(position)).getSpaTitle().equalsIgnoreCase(
		// "All Treatments")) {
		// SpasModel md = SpasModel.getInstance();
		// spaChildList = md.getAllSpaSections(SpaActivity.this);
		// adapter = new SpaAdapter(SpaActivity.this, 0, spaChildList);
		// spaElv.setAdapter(adapter);
		// tv_title_spa.setText("All Treatments");
		// spAdapter.selectedString = "All Treatments";
		// } else {
		// tv_title_spa
		// .setText(((SpaParentDTO) SpaActivity.this.spaParentList
		// .get(position)).getSpaTitle());
		// spAdapter.selectedString = ((SpaParentDTO)
		// SpaActivity.this.spaParentList
		// .get(position)).getSpaTitle();
		// SpasModel md = SpasModel.getInstance();
		// spaChildList = md.getSpaSection(
		// ((SpaParentDTO) SpaActivity.this.spaParentList
		// .get(position)).get_id(), SpaActivity.this);
		// adapter.refreshData(spaChildList);
		// }
		// }
		//
		// @Override
		// public void onNothingSelected(AdapterView<?> parent) {
		// // TODO Auto-generated method stub
		//
		// }
		// });
		// if (spaParentList != null) {
		// if (spaParentList.size() > 0) {
		// SpaParentDTO parent = new SpaParentDTO();
		// parent.setSpaTitle("All Treatments");
		// spaParentList.add(0, parent);
		// spAdapter = new SpinnerAdapter(SpaActivity.this, 0,
		// spaParentList);
		// // spinner.setAdapter(spAdapter);
		// spAdapter.notifyDataSetChanged();
		// }
		// }
		spaElv.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				int i = adapter.getGroupCount();
				for (int j = 0; j < i; j++) {
					if (j != groupPosition) {
						spaElv.collapseGroup(j);
					}
				}

			}
		});

	}

	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		arrChild.clear();
		if (charText.length() == 0) {
			arrChild.addAll(spaChildList);
			adapter.notifyDataSetChanged();
		} else {
			for (SpaChildDTO children : spaChildList) {
				if (children.getSecTitle().toLowerCase(Locale.getDefault())
						.contains(charText)) {
					arrChild.add(children);
					adapter.refreshData(arrChild);
				}
			}
		}

	}

	private void initViews() {
		tv_actionbar_title = (TextView) findViewById(R.id.tv_actionbar_title);
		tv_actionbar_title.setText("Spa");
		tv_actionbar_title.setTextColor(getResources().getColor(
				R.color.spad_color));
		spaElv = (ExpandableListView) findViewById(R.id.elv_main_spa);
		this.menuView = findViewById(R.id.menu_container);
		this.menuViewSecondary = findViewById(R.id.menu_secondary);
		this.blurimageView = (LinearLayout) findViewById(R.id.lay_blur_image);

		footer = getLayoutInflater().inflate(R.layout.activity_footer, null);
		header = getLayoutInflater()
				.inflate(R.layout.activity_spa_header, null);
		this.mPager = (ViewPager) header.findViewById(R.id.pager);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int height = displaymetrics.heightPixels;
		mPager.getLayoutParams().height = height*1/3;
		mIndicator = (CirclePageIndicator) header.findViewById(R.id.indicator);
		tv_title_spa = (TextView) header.findViewById(R.id.tv_title_spa);
		edSpa = (EditText) header.findViewById(R.id.edSpa);
		// spinner = (Spinner) header.findViewById(R.id.sp_spinner_spa);
		spaElv.addHeaderView(header);
		spaElv.addFooterView(footer);

	}

	@Override
	public void onMenuSelected(int paramInt) {
		// TODO Auto-generated method stub

	}

	private class ImagePagerAdapter extends PagerAdapter {
		private int[] mImages = new int[] { R.drawable.spa1, R.drawable.spa2,
				R.drawable.spa3 };

		@Override
		public int getCount() {
			return mImages.length;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Context context = SpaActivity.this;
			ImageView imageView = new ImageView(context);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setImageResource(mImages[position]);

			((ViewPager) container).addView(imageView, 0);
			return imageView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((ImageView) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return (view == object);
		}

		/**
		 * Called when the a change in the shown pages has been completed. At
		 * this point you must ensure that all of the pages have actually been
		 * added or removed from the container as appropriate.
		 * 
		 * @param arg0
		 *            The containing View which is displaying this adapter's
		 *            page views.
		 */
		@Override
		public void finishUpdate(ViewGroup arg0) {
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(ViewGroup arg0) {
		}
	}
}
